#pragma once

#include<iostream>
#include<unordered_map>
#include<functional>
#include<time.h>
using namespace std;

using task_t = function<void()>;
static int number = 0;

void DownLoad()
{
    cout<<"我是下载任务"<<endl;
}
void Log()
{
    cout<<"我是日志任务"<<endl;
}
void Sql()
{
    cout<<"我是数据库同步任务"<<endl;
}
class TaskManger
{
public:
    TaskManger()
    {
        srand(time(nullptr));
        InsertTask(DownLoad);
        InsertTask(Log);
        InsertTask(Sql);
    }
    void InsertTask(task_t t)
    {
        tasks[number++] = t;
    }
    int SelectTask()
    {
        return rand()%number;
    }
    void Excute(int number)
    {
        if(tasks.find(number) == tasks.end())   return;
        tasks[number]();//执行任务
    }
    ~TaskManger(){}
private:
    unordered_map<int, task_t> tasks;

};

TaskManger tm;

void Worker()
{
    while(1)
    {
        int cmd = 0;
        int n = read(0,&cmd, sizeof(cmd));
        if(n == sizeof(cmd))
        {
            tm.Excute(cmd);
        }
        else if(n == 0)
        {
            cout<<"pid:"<<getpid()<<" quit..."<<endl;
            break;
        }
        else
        {

        }
    }
}